package action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import service.IProductclassService;
import vo.Productclass;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
public class ProductclassAction extends ActionSupport implements ModelDriven<Productclass>{
	private int page;
	private int rows;
	
	private IProductclassService productclassService;
	public Productclass productclass=new Productclass();
	
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public Productclass getProductclass() {
		return productclass;
	}
	public void setProductclass(Productclass productclass) {
		this.productclass = productclass;
	}
	
	//=================前台返回Json对象============================
		
		//查找所有的学生信息并分页
	
		//========================================================
	
    
	
	
	public IProductclassService getProductclassService() {
		return productclassService;
	}
	public void setProductclassService(IProductclassService productclassService) {
		this.productclassService = productclassService;
	}
	
   //====================1.显示所有商品=============================== 
    public String showproductclass() throws Exception{
    	
    	String hql=createHql();
    	List productclass1=productclassService.getProductclass(page, rows,hql);
    	int total=productclassService.getAllProductclass(hql);
    	Map<String,Object>  map=new HashMap<String,Object>();
    	map.put("total", total);
    	map.put("rows", productclass1);
    	writeJson(map);  
    	
    	 productclass=new Productclass();
    	
    	
    	return null;
    	
    }
    
	
	//===================2.添加商品==========================================
    public String addproductclass(){
    	
    	
      Result result = new Result();
        try {
        	productclassService.addProductclass(productclass);
            result.setSuccess(true);
            result.setMsg("添加商品成功");
          
        } catch (Exception e) {
            result.setMsg("添加商品失败");
        }
        writeJson(result);
        productclass=new Productclass();
		
   	
   	return null;
    	
    }
    
	
	//======3.删除商品============
    public String delproductclass(){
    	
    	
    	 Result result = new Result();
         try {
        	 productclassService.delProductclass(productclass.getPclassId());
             result.setSuccess(true);
             result.setMsg("成功删除一条记录");
           
         } catch (Exception e) {
             result.setMsg("删除失败");
         }
         writeJson(result);
         productclass=new Productclass();
		
    	
    	return null;
    	
    }
	
	//======4.编辑商品============
    public void editproductclass(){
		
	    	
	      Result result = new Result();
	        try {
	        	 productclassService.editProductclass(productclass);
	            result.setSuccess(true);
	            result.setMsg("成功修改一条商品记录");
	          
	        } catch (Exception e) {
	            result.setMsg("删除失败");
	        }
	        writeJson(result);
	        productclass=new Productclass();
	        
			
	   	
	   
	    	
		
	}
	
	//======5.显示商品类别的combobox============
    public String showpclasscombobox() throws Exception{
        System.out.println("产品类别1");
    	List<Productclass> productclass1=productclassService.getAllPclassObject();
    	   System.out.println("产品类别2"+productclass1);
    	   
    	writeJson(productclass1);
    	 System.out.println("产品类别3");
    	return null;
    	
    }
    
	
	//======6.获得所有的productclass============
    //========7.1向前台输出json的writeJson方法====================
    public void writeJson(Object object) {
        try {         
        	 System.out.println("json数据1");
        	JSONArray jsonObjectFromMap = JSONArray.fromObject(object);
        	 System.out.println("json数据2");
        	String out=jsonObjectFromMap.toString();
        	 System.out.println("json数据3");
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            System.out.println("json数据4");
            ServletActionContext.getResponse().getWriter().write(out);
            System.out.println("json数据6");
            ServletActionContext.getResponse().getWriter().flush();
            System.out.println("json数据7");
        } catch (IOException e) {
            System.out.println("json数据出错");
            }
     
    }
    //========7.2获得查询的hql语句====================
    public String createHql(){
          String hql="from Productclass p where 1=1 ";
            if(productclass.getPclassId()!=null)
				hql+=" and p.pclassId="+productclass.getPclassId();
			if(productclass.getPclassName()!=null)
				hql+=" and p.pclassName like '%"+productclass.getPclassName()+"%'";
				return hql;
    	
    }
	public Productclass getModel() {
		
		return productclass;
	}
	
    
}
